#!/bin/bash

date=`date`

application_name=$1
script_path=$2
log_path=$3

echo $application_name

#Get yarn application info by application_name
info=`yarn application -list |grep ${application_name}`
echo $info
if [[ $info -eq '' ]];
then
  echo "$date $application_name is not running, application will start..."
	nohup ${script_path} > ${log_path} 2>&1 &
	exit 0
fi

#Get flink job url
array=(${info/\\t/ })
flink_job_url=${array[9]}
yarn_application_id=${array[0]}

#Get flink job status
job_status=`curl -XGET ${flink_job_url}/jobs | grep -Po 'status[" :]+\K[^"]+'`
echo $job_status

if [[ $job_status -ne RUNNING ]];
then
  yarn application -kill ${yarn_application_id}
	echo "$date $application_name is not running, application will start..."
	nohup ${script_path} > ${log_path} 2>&1 &
else
	echo "$date $application_name is running..."
fi